<?php
require_once(dirname(__FILE__) . '/Amazon/AmazonCall.php');

class Book
{
	public static function getMatches($string, $condition)
	{
		mb_regex_encoding('UTF-8');
		mb_ereg_search_init($string, $condition);
		$result = mb_ereg_search();
		$return = mb_ereg_search_getregs();
		array_shift($return);
		return $return;
	}
	
	public static function checkMatch($string, $condition)
	{
		mb_regex_encoding('UTF-8');
		
		mb_ereg_search_init($string, $condition);
		$result = mb_ereg_search();
		if($result)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public static function execute($botObj, $tweet, $action_value)
	{
		mb_internal_encoding("UTF-8");
		mb_regex_encoding("UTF-8");
		
		$tweet['message'] = mb_ereg_replace("@{$botObj->config['bot_name']}\s", "", $tweet['message']);
	
		list($input_author) = self::getMatches($tweet['message'], "(.+)さんの本はどう思う？");
		
		if(!$input_author)
			return false;
	
		$amazonAPI = new AmazonProductAPI($botObj->config['aws_key'], $botObj->config['aws_secret']);
		
		// Try to get a result back.  Error out if we don't get a valid response.
		try
		{
			$return_xml = $amazonAPI->searchAuthor($input_author);
		}
		catch (Exception $e)
		{
			$botObj->pluginResponse['tweet'] = "ごめんなさい、今{$input_author}さんの本を調べません…";
			return;
		}
		
		$found_author = false;
		$valid_length = false;
		
		foreach($return_xml->Items->Item as $item)
		{
			$asin = $item->ASIN;
			$author = $item->ItemAttributes->Author;
			$title = $item->ItemAttributes->Title;
			$found_author = true;
			
			// Length of bot response without titles = 38 + 17 for @[Username(max 15)] with a space = 55
			// This gives us 85 characters to work with. There are 4 values that need to be filled in so we
			// average out the remaining characters to get 21.25, or 21 rounded down
			if(mb_strlen($author) <= 21 && mb_strlen($title) <= 21)
			{
				$valid_length = true;
				break;
			}
		}
		
		// No titles were found for this author
		if(!$found_author)
		{
			$botObj->pluginResponse['tweet'] = "ごめんなさい、{$input_author}さんはご存知ません…";
			return;
		}
		// The tweet is too long
		if(!$valid_length)
		{
			$botObj->pluginResponse['tweet'] = "ごめんなさい、{$input_author}さんの詳しくは沢山書いてあるのでツイートは出来ません。";
			return;
		}
		
		// If the similiar items search fails, at least show the original author information
		try
		{
			$similiar = $amazonAPI->getSimiliar($asin);
		}
		catch (Exception $e)
		{
			$botObj->pluginResponse['tweet'] = "{$input_author}さんの本と言えば「{$title}」ですね。でも、「{$title}」と同じ感じの本はご存知ません…";
			return;
		}
		
		$foundAuthor = false;
		
		foreach($similiar->Items->Item as $book)
		{
			// I wanted the similiar items search to also show a different author, so we check for that here as well
			if($book->ItemAttributes->Author != $author && mb_strlen($book->ItemAttributes->Author) <= 21 && mb_strlen($book->ItemAttributes->Title) <= 21)
			{
				$foundAuthor = true;
				
				$alternative_author = $book->ItemAttributes->Author;
				$alternative_title = $book->ItemAttributes->Title;
				break;
			}
		}
		
		// Couldn't find any similiar titles, so at least show the original author information
		if(!$foundAuthor)
		{
			$botObj->pluginResponse['tweet'] = "{$input_author}さんの本と言えば「{$title}」ですね。でも、「{$title}」と同じ感じの本はご存知ません…";
			break;
		}
		// Everything checks out, go ahead and show the full details!
		else
		{
			//三島 由紀夫さんの本はどう思う？
			$botObj->pluginResponse['tweet'] = "{$input_author}さんの本と言えば「{$title}」ですね。後{$input_author}さんの本が好きなら{$alternative_author}さんの「{$alternative_title}」もおすすめですよ♪";
		}
	}
}
?>